//
//  AmakeAccViewController.m
//  Distributor
//
//  Created by 主事丫环 on 2019/5/14.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "AmakeAccViewController.h"
#import "HFAddAddressViewController.h"
#import "CSPersonModel.h"
#import "HFAddressModel.h"
@interface AmakeAccViewController ()
@property (weak, nonatomic) IBOutlet UILabel *nameLb;
@property (weak, nonatomic) IBOutlet UILabel *phoneLB;
@property (weak, nonatomic) IBOutlet UIImageView *myimage;
@property (weak, nonatomic) IBOutlet UILabel *adressLb;
@property (weak, nonatomic) IBOutlet UILabel *mytitilelb;
@property (weak, nonatomic) IBOutlet UILabel *numberLB;
@property(nonatomic,strong)CSPersonModel *addressmodel;

@end

@implementation AmakeAccViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"提交订货单";
    // Do any additional setup after loading the view.
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    self.addressmodel = [[CSPersonModel alloc]init];
    [self requestaddress];
    // Do any additional setup after loading the view from its nib.
}
-(void)shwodietaile{
    
    if ([_model.image_url hasPrefix:@"http"]) {
        [self.myimage sd_setImageWithURL:[NSURL URLWithString:_model.image_url] placeholderImage:defaultImg];
    }else{
        sdimg(_myimage, _model.image_url);
        
    }
    
    if (strIsEmpty(_addressmodel.address)) {
        
        UIAlertController *alter = [UIAlertController alertControllerWithTitle:@"您暂未设置地区" message:@"请先添加地址" preferredStyle:(UIAlertControllerStyleAlert)];
        
        UIAlertAction *act1 = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
        UIAlertAction *act2 = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            HFAddAddressViewController *addres = [[HFAddAddressViewController alloc]init];
            addres.myBlock = ^(NSString * _Nonnull str) {
                [self requestaddress];
            };
        }];
        
        [alter addAction:act1];
        [alter addAction:act2];
        [self presentViewController:alter animated:YES completion:nil];
        
 
        return;
    }
    
    self.nameLb.text = [NSString stringWithFormat:@"%@",_addressmodel.person];
    self.phoneLB.text = [NSString stringWithFormat:@"%@",_addressmodel.phone];
    self.adressLb.text = [NSString stringWithFormat:@"%@",_addressmodel.address];

    self.mytitilelb.text = [NSString stringWithFormat:@"%@",_model.parts_name];
    
    self.numberLB.text = [NSString stringWithFormat:@"共计%@个",_model.mycoun];

}

- (IBAction)seleaddressAction:(UIButton *)sender {
    HFAddAddressViewController *addres = [[HFAddAddressViewController alloc]init];
    
    HFAddressModel *model = [[HFAddressModel alloc] init];
    
    model.areaAdd =self.addressmodel.areaAdd;
    model.address =self.addressmodel.address;
    model.area =self.addressmodel.area;
    addres.model =model;
    addres.myBlock = ^(NSString * _Nonnull str) {
        [self requestaddress];
    };

    
    
    [self.navigationController pushViewController:addres animated:YES];
    
}


- (IBAction)tijaioactin:(UIButton *)sender {
    if (strIsEmpty(_addressmodel.address)) {
        
        UIAlertController *alter = [UIAlertController alertControllerWithTitle:@"您暂未设置地区" message:@"请先添加地址" preferredStyle:(UIAlertControllerStyleAlert)];
        
        UIAlertAction *act1 = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
        UIAlertAction *act2 = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            HFAddAddressViewController *addres = [[HFAddAddressViewController alloc]init];
            addres.myBlock = ^(NSString * _Nonnull str) {
                [self requestaddress];
            };
        }];
        
        [alter addAction:act1];
        [alter addAction:act2];
        [self presentViewController:alter animated:YES completion:nil];
        
        
        return;
    }
   
    
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
    param[@"id"] = NOTNIL(_model.Id);
   
    
    param[@"count"] = NOTNIL(_model.mycoun);
    
    param[@"address"] = NOTNIL(_addressmodel.area);
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [NetWorkTool POST:agentPlaceOrder param:param success:^(id dic) {
        ShowToastWithText(@"订货成功");
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"refashOrder" object:nil userInfo:nil];
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self.navigationController popViewControllerAnimated:YES];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
    } fail:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
    } needUser:YES];

}
-(void)requestaddress{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [NetWorkTool POST:CcentenInfo param:nil success:^(id dic) {
        [self.addressmodel setValuesForKeysWithDictionary:[dic objectForKey:@"data"]];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self shwodietaile];

        
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self shwodietaile];

        
    } fail:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self shwodietaile];

        
    } needUser:YES];
}

@end
